package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Bills;
import com.ruoyi.system.service.IBillsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 账单记录Controller
 * 
 * @author ruoyi
 * @date 2024-07-02
 */
@RestController
@RequestMapping("/system/bills")
public class BillsController extends BaseController
{
    @Autowired
    private IBillsService billsService;

    /**
     * 查询账单记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:bills:list')")
    @GetMapping("/list")
    public TableDataInfo list(Bills bills)
    {
        startPage();
        List<Bills> list = billsService.selectBillsList(bills);
        return getDataTable(list);
    }

    /**
     * 导出账单记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:bills:export')")
    @Log(title = "账单记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Bills bills)
    {
        List<Bills> list = billsService.selectBillsList(bills);
        ExcelUtil<Bills> util = new ExcelUtil<Bills>(Bills.class);
        util.exportExcel(response, list, "账单记录数据");
    }

    /**
     * 获取账单记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:bills:query')")
    @GetMapping(value = "/{billId}")
    public AjaxResult getInfo(@PathVariable("billId") Long billId)
    {
        return success(billsService.selectBillsByBillId(billId));
    }

    /**
     * 新增账单记录
     */
    @PreAuthorize("@ss.hasPermi('system:bills:add')")
    @Log(title = "账单记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Bills bills)
    {
        IdentifierGenerator generator = new IdentifierGenerator();
        bills.setBillSystemSerial(generator.generateBillSerialNumber());// 账单序列号
        bills.setPaymentSerialNumber(generator.generatePaymentSerialNumber());// 支付流水号
        bills.setInvoiceId(generator.generateInvoiceCode());// 发票编码
        return toAjax(billsService.insertBills(bills));
    }

    /**
     * 修改账单记录
     */
    @PreAuthorize("@ss.hasPermi('system:bills:edit')")
    @Log(title = "账单记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Bills bills)
    {
        return toAjax(billsService.updateBills(bills));
    }

    /**
     * 删除账单记录
     */
    @PreAuthorize("@ss.hasPermi('system:bills:remove')")
    @Log(title = "账单记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{billIds}")
    public AjaxResult remove(@PathVariable Long[] billIds)
    {
        return toAjax(billsService.deleteBillsByBillIds(billIds));
    }
}
